<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>体检预约</title>
    <meta name="description" content="体检预约">
    <meta name="keywords" content="体检预约">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 16px;
            color: #8c939d;
            width: 50px;
            height: 50px;
            line-height: 50px;
            text-align: center;
        }
        .avatar {
            width: 50px;
            height: 50px;
            display: block;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                <el-button @click="findPage(1)" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table
                    size="small"
                    :data="dataList"
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="id"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                    stripe highlight-current-row>
                <el-table-column prop="priority" label="序号" align="center"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="访问路径" align="center"></el-table-column>
                <el-table-column prop="icon" label="图标" align="center"></el-table-column>
                <el-table-column prop="description" label="描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="上传图标">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/menu/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <!--用于上传图片的预览-->
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <!--用于展示上传的图标-->
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="父菜单名称">
                                    <el-select v-model="id" clearable placeholder="请选择">
                                        <el-option
                                                v-for="menu in menus"
                                                :key="menu.id"
                                                :label="menu.name"
                                                :value="menu.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="上传图标">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/menu/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <!--用于上传图片的预览-->
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <!--用于展示上传的图标-->
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="父菜单名称">
                                    <el-select v-model="id" v-bind:disabled="select_d" clearable placeholder="请选择">
                                        <el-option
                                                v-for="menu in menus"
                                                :key="menu.id"
                                                :label="menu.name"
                                                :value="menu.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            autoUpload:true,//自动上传
            imageUrl:null,  // 用于上传后的图片预览
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            rules: {//校验规则
                name: [{ required: true, message: '菜单名称为必填项', trigger: 'blur' }]
            },
            menus: [],
            id: '',
            select_d:''
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                this.imageUrl = "http://qt5mmly2k.hd-bkt.clouddn.com/"+response.data;
                this.$message({
                    message:response.message,
                    type:response.flag ? 'success' : 'error'
                });
                // 设置模型数据（图片名称）,用于后续提交套餐基本信息时保存到数据库
                this.formData.icon = response.data;
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            //编辑
            handleEdit() {
                // 校验表单输入项是否合法
                this.formData.parentMenuId = this.id;
                this.$refs['dataEditForm'].validate((valid)=>{
                    if (valid){
                        axios.post("/menu/edit.do",this.formData)
                            .then((response)=>{
                                this.dialogFormVisible4Edit = false;
                                if (response.data.flag){
                                    this.$message({
                                        type:'success',
                                        message:response.data.message
                                    });
                                }else {
                                    this.$message.error(response.data.message);
                                }
                            })
                            .catch((error)=>{
                                console.log("服务器响应失败"+error);
                            })
                            .finally(()=>{
                                this.findPage();
                            });
                    }
                });
            },
            //添加
            handleAdd () {
                this.formData.parentMenuId = this.id;
                // 校验表单输入项是否合法
                this.$refs['dataAddForm'].validate((valid)=>{
                    if (valid){
                        // 表单数据通过校验，发送ajax请求将表单数据提交到后台
                        axios.post("/menu/add.do",this.formData)
                            .then((response)=>{
                                // 隐藏新增窗口
                                this.dialogFormVisible = false;
                                if (response.data.flag){
                                    this.$message({
                                        message:response.data.message,
                                        type:'success'
                                    });
                                }else{
                                    this.$message.error(response.data.message);
                                }
                            })
                            .catch((error)=>{
                                console.log("服务器响应失败"+error);
                            })
                            .finally(()=>{
                                this.findPage();
                            });
                    }
                });
            },
            //分页查询
            findPage(page) {
                if (page != undefined){
                    this.pagination.currentPage = page;
                }

                // 分页参数
                var param = {
                    currentPage:this.pagination.currentPage, // 当前页码
                    pageSize: this.pagination.pageSize, // 每页显示的条数
                    queryString: this.pagination.queryString  // 查询条件
                };

                // 发送ajax请求
                axios.post("/menu/findPage.do",param)
                    .then((response)=>{
                        this.dataList = response.data.rows;
                        this.pagination.total = response.data.total;
                    })
                    .catch((error)=>{
                        console.log("服务器响应失败"+error);
                    });

            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.imageUrl = null;
            },
            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
                this.findOneAll();
            },
            findOneAll(){
                // 发送ajax请求查询所有一级菜单信息
                axios.get("/menu/findOneAll.do").then((response)=>{
                    if (response.data.flag){
                        this.menus = response.data.data;
                    }else{
                        this.$message.error(response.data.message);
                    }
                });
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.dialogFormVisible4Edit = true;
                /*this.formData = row;
                this.imageUrl = "http://qt5mmly2k.hd-bkt.clouddn.com/"+row.icon;*/
                // alert(row.id);
                // 发送ajax请求
                axios.get("/menu/findById.do?id="+row.id)
                    .then((response)=>{
                        if (response.data.flag){
                            this.formData = response.data.data;
                            if (this.formData.parentMenuId == null){
                                this.select_d = "disabled";
                            }
                            this.imageUrl = "http://qt5mmly2k.hd-bkt.clouddn.com/"+this.formData.icon;
                            this.findOneAll();
                            this.id = this.formData.parentMenuId;
                        }else{
                            this.$message.error(response.data.message);
                        }
                    })
                    .catch((error)=>{
                        console.log("服务器响应失败"+error);
                    });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm("确认删除吗？","提示",{type:'warning'}).then(()=>{
                    //点击确定按钮时只需此处代码
                    //alert('用户点击的是确定按钮');
                    axios.get('/menu/delete.do?id='+row.id)
                        .then((response)=>{
                            if (response.data.flag){
                                // 删除成功
                                this.$message({
                                    message:response.data.message,
                                    type:'success'
                                });
                            }else{
                                this.$message.error(response.data.message);
                            }
                        })
                        .catch((error)=>{
                            console.log("服务器响应失败"+error);
                        })
                        .finally(()=>{
                            this.findPage();
                        });
                });
            }
        }
    })
</script>
</html>